package dasregistry.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.biojava.dasobert.das2.io.DasSourceWriter;
import org.biojava.dasobert.das2.io.DasSourceWriterImpl;
import org.biojava.dasobert.dasregistry.DasCoordinateSystem;
import org.biojava.dasobert.util.HttpConnectionTools;
import org.biojava.utils.xml.PrettyXMLWriter;

public class CreateCoordinates {
	public static final String XML_CONTENT_TYPE = "application/xml";
	
	public static void main(String [] args){
		System.out.println("running new createCoords");
		CreateCoordinates creator=new CreateCoordinates();
		DasCoordinateSystem[] coords=creator.createCoordinates();
		creator.submitCoordinatesToRegistry(coords);
		
	}
	
	
	
	
	private DasCoordinateSystem[] createCoordinates(){
		
DasCoordinateSystem [] coordinates=new DasCoordinateSystem[1];
		//need to put create coordinate xml  in here from CoordinateSystem objects?
        DasCoordinateSystem coord=new DasCoordinateSystem();
        coord.setAuthority("CalJacTes");
        coord.setNCBITaxId(9483);
        coord.setOrganismName("Callithrix jacchus");
       coord.setVersion("");
        coord.setCategory("supercontig");
       System.out.println(coord.toString());
        coordinates[0]=coord;
        
       // <COORDINATES uri="http://www.dasregistry.org/dasregistry/coordsys/CS_DS43" taxid="9606" source="Clone" authority="NCBI" test_range="" version="36">NCBI_36,Clone,Homo sapiens</COORDINATES>
       // System.out.
        
        
        
//        <DASCOORDINATESYSTEM>
//        <COORDINATES source="Protein Sequence" authority="Ensembl" 
//      taxid="9483">Ensembl,Protein Sequence,Callithrix jacchus</COORDINATES>
//      </DASCOORDINATESYSTEM>
        return coordinates;
	}
	private void submitCoordinatesToRegistry(DasCoordinateSystem [] coords){
	    
       
            URL urlLocal=null;
            
			try {
				//urlLocal = new URL("http://localhost:8080/dasregistry/das/coordinatesystem");
				urlLocal = new URL("http://www.dasregistry.org/das/coordinatesystem");//"http://mib21789i.internal.sanger.ac.uk:8080/dasregistry/das/coordinatesystem");
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        
           
            HttpURLConnection conn=null;
			try {
				conn = HttpConnectionTools.openHttpURLConnection(urlLocal);
			} catch (ConnectException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	              
            
            try {
				conn.setRequestMethod("POST");
			
            conn.setRequestProperty("Content-Type", "text/xml; charset=\"utf-8\"");
            conn.setDoOutput(true);
	        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
	       
			PrintWriter pw = new PrintWriter(wr);
			PrettyXMLWriter xw = new PrettyXMLWriter(pw);
			xw.printRaw("<?xml version='1.0' encoding='UTF-8' ?>");

			xw.openTag("DASCOORDINATESYSTEM");
			DasSourceWriter writer = new DasSourceWriterImpl();
			for (int i = 0; i < coords.length; i++) {
				DasCoordinateSystem coord = coords[i];				
				writer.writeCoordinateSystem(xw, coord);
				System.out.println("writing coordinate system!!!");
			}
			xw.closeTag("DASCOORDINATESYSTEM");
			wr.flush();
			
	        // Get the response
	        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
	        String line;
	        System.out.println("writing response");
	        while ((line = rd.readLine()) != null) {
	            // Process line...
	        	System.out.println(line);
	        }
	        wr.flush();
	        wr.close();
	        xw.close();
	        
            } catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	
}
